package com.syh.jn.scm.service.impl.downstream.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.system.DownAccount;
import com.syh.jn.scm.domain.downstream.entity.system.DownRole;
import com.syh.jn.scm.domain.downstream.repository.system.DownAccountRepository;
import com.syh.jn.scm.domain.downstream.repository.system.DownRoleRepository;
import com.syh.jn.scm.dto.downstream.system.DownMenuVo;
import com.syh.jn.scm.dto.downstream.system.downRole.DownRoleMto;
import com.syh.jn.scm.dto.downstream.system.downRole.DownRoleSto;
import com.syh.jn.scm.dto.downstream.system.downRole.DownRoleVo;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.downstream.system.DownRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DownRoleServiceImpl extends AbstractServiceImpl<DownRoleSto, DownRoleMto, DownRoleVo, DownRole, Long> implements DownRoleService {

  @Autowired
  private DownRoleRepository downRoleRepository;

  @Override
  protected AggregateRepository<DownRole, Long> getAggregateRepository() {
    return this.downRoleRepository;
  }

  /**
   * 更新角色菜单
   *
   * @param roleId
   * @param menuIds
   */
  @Override
  public void updateRoleMenus(Long roleId, List<Long> menuIds) {
    DownRole role = downRoleRepository.getById(roleId);
    ExtAssert.notNull(role, "对象不存在");
    role.updateMenus(menuIds);
    downRoleRepository.update(role, true);
  }

  @Override
  public List<PermissionVo> getPermissionByRoleId(Long roleId) {
    return downRoleRepository.getPermissionByRoleId(roleId);
  }

  @Override
  public Long save(DownRoleSto sto) {
    DownRole downRole = new DownRole(sto);
    return downRoleRepository.add(downRole);
  }

  @Override
  public void update(DownRoleMto downRoleMto) {
    ExtAssert.notNull(downRoleMto, "对象为空");
    ExtAssert.notNull(downRoleMto.getId(), "id为空");
    DownRole downRole = downRoleRepository.getById(downRoleMto.getId());
    downRole.modify(downRoleMto);
    downRoleRepository.update(downRole);
  }

  @Override
  public void delete(Long id) {
    DownRole downRole = downRoleRepository.getById(id);
    if (downRole == null) {
      return;
    }
    downRole.destroy();
    downRoleRepository.removeById(id);
  }

  @Override
  public DownRoleVo getRole(Long accountId) {
    ExtAssert.notNull(accountId, "id为空");
    DownAccountRepository downAccountRepository = (DownAccountRepository) SpringContextUtils.getBean(DownAccountRepository.class);
    DownAccount downAccount = downAccountRepository.getById(accountId);
    if (downAccount == null) {
      throw new BusinessException("该账号无数据存在");
    }
    DownRole role = downRoleRepository.getById(downAccount.getRoleId());
    if (role == null) {
      return null;
    }
    DownRoleVo roleVo = new DownRoleVo();
    ExtBeanUtils.copyProperties(role, roleVo);
    return roleVo;
  }

  /**
   * 获取角色菜单
   *
   * @param wrapper
   */
  @Override
  public List<DownMenuVo> getMenus(Wrapper wrapper) {
    return this.downRoleRepository.getMenus(wrapper);
  }

  @Override
  public List<DownRoleVo> getAllRolesWithPermission() {
    List<DownRoleVo> roles = downRoleRepository.getAllRolesWithPermission();
    return roles;
  }

  @Override
  public List<DownRoleVo> getRolesByPermissionId(Long perId) {
    return downRoleRepository.getRolesByPermissionId(perId).stream().map(role -> {
      DownRoleVo roleVo = new DownRoleVo();
      ExtBeanUtils.copyProperties(role, roleVo);
      return roleVo;
    }).collect(Collectors.toList());
  }
}
